import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import '@/router/before'
import { createPinia } from 'pinia'
import antd from 'ant-design-vue'
import UsePro from './utils/UsePro'
import { assets } from '@/utils/static'
import cronAnt from '@vue-js-cron/ant'
import '@vue-js-cron/ant/dist/ant.css'
import 'normalize.css'
import { keyboard } from '@/directives/index'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'

const pinia = createPinia()

pinia.use(piniaPluginPersistedstate)

const app = createApp(App)
app
  .use(router)
  .use(pinia)
  .use(antd)
  .use(UsePro)
  .use(cronAnt)
  .directive('keyboard', keyboard)
  .provide('useStaticImg', assets)
  .mount('#app')

// `trace` 是组件层级结构的追踪
app.config.warnHandler = (msg, instance, trace) => {

}

// 全局注册一些辅助性的函数 desc在 global.d.ts下
window.Map.prototype.toArray = function () {
  if (this === null || this === undefined) return this
  return Array.from(this, ([key, value]) => {
    if (typeof value === 'object') {
      return { key, ...value }
    } else {
      return { key, value }
    }
  })
}

window.Array.prototype.toMap = function (key: string, valueKey?: string): Map<string, any> {
  if (this && typeof this[0] !== 'object') throw new Error('仅支持Record<string, any>[]格式的数组')
  else if (this == null || this === undefined) return this

  const _map = new Map()
  this.forEach(item => {
    _map.set(item[key], valueKey ? item[valueKey] : item)
  })

  return _map
}
